﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Web.czk
{
    /// <summary>
    /// zb 的摘要说明
    /// </summary>
    public class zb : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string sn = string.IsNullOrEmpty(context.Request.Form["sn"]) ? context.Request.QueryString["sn"] : context.Request.Form["sn"];
            string zbj = string.IsNullOrEmpty(context.Request.Form["zbj"]) ? context.Request.QueryString["zbj"] : context.Request.Form["zbj"];
            string IP = LJSheng.Common.LCommon.GetIPAddress(context);
            string returnstr = "请联系QQ9021616 购买注册";
            //限制IPSN登录
            string ckip = ReadFile(System.Web.HttpContext.Current.Server.MapPath("/live/ip/") + "ip.html");
            if (ckip.Contains(sn) || (!string.IsNullOrEmpty(IP) && ckip.Contains(IP)))
            { returnstr = "QQ9021616"; }
            else
            {
                StreamWriter sr = null;
                try
                {
                    //启动日志
                    string qdpath = System.Web.HttpContext.Current.Server.MapPath("/czk/qd/") + sn + ".html";
                    if (!File.Exists(qdpath))
                    {
                        sr = File.CreateText(qdpath);
                        sr.WriteLine(DateTime.Now.ToString());
                    }
                    else
                    {
                        File.AppendAllText(qdpath, DateTime.Now.ToString() + " - " + "zbj=" + zbj + "\r\n");
                    }

                    //读取注册码
                    returnstr = ReadFile(System.Web.HttpContext.Current.Server.MapPath("/czk/") + sn + ".html");
                    if (string.IsNullOrEmpty(returnstr))
                    {
                        //没有授权信息
                        returnstr = "http://520299.com";
                        StreamWriter db = null;
                        try
                        {
                            string dbpath = System.Web.HttpContext.Current.Server.MapPath("/TB/mm/") + sn + ".html";
                            if (!File.Exists(dbpath))
                            {
                                db = File.CreateText(dbpath);
                                db.WriteLine(DateTime.Now.ToString());
                            }
                            else
                            {
                                File.AppendAllText(dbpath, DateTime.Now.ToString() + " - " + "zbj=" + zbj + ",IP=" + LJSheng.Common.LCommon.GetIPAddress(context) + "\r\n");
                            }
                        }
                        catch
                        {
                            //returnstr = "http://520299.com";
                        }
                        finally
                        {
                            if (db != null)
                            {
                                db.Close();
                                db.Dispose();
                            }
                        }
                    }
                }
                catch
                {
                    returnstr = "http://520299.com";
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                        sr.Dispose();
                    }
                }
                //同IP记录
                using (StreamWriter sw = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("/czk/ip/") + IP + ".html", true, Encoding.UTF8))
                {
                    sw.Write(DateTime.Now.ToString() + "-" + sn + " - " + zbj + "\r\n");
                    sw.Close();
                    sw.Dispose();
                }
            }
            context.Response.Write(returnstr.Replace("\r", "").Replace("\n", ""));
            context.Response.End();
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private string ReadFile(string path)
        {
            string s = "";
            if (File.Exists(path))
            {
                StreamReader myreader = null;
                try
                {
                    myreader = File.OpenText(path);
                    s = myreader.ReadToEnd();
                }
                catch
                {
                    s = "http://520299.com";
                }
                finally
                {
                    if (myreader != null)
                    {
                        myreader.Close();
                        myreader.Dispose();
                    }
                }
            }
            return s;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}